export default class Dep {

  static target = null
  static targetStack = []

  constructor() {
    this.subs = new Set()
  }

  depend() {
    if (Dep.target) this.addSub(Dep.target)
  }

  addSub(sub) {
    this.subs.add(sub)
  }

  notify() {
    for (const sub of this.subs) {
      sub.update()
    }
  }

  static pushTarget(target) {
    Dep.targetStack.push(Dep.target)
    Dep.target = target
  }

  static popTarget() {
    Dep.target = Dep.targetStack.pop()
  }

}
